package com.ld.common.tag;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.ld.shieldsb.common.core.util.StringUtils;

public class SuperQueryTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        JspWriter out = this.getJspContext().getOut();// 指定输入流
        HttpServletRequest request = (HttpServletRequest) ((PageContext) this.getJspContext()).getRequest();
        JspFragment jf = this.getJspBody();
        StringWriter sw = new StringWriter();
        jf.invoke(sw);
        String content = sw.getBuffer().toString();
        StringBuffer sb = new StringBuffer();
        sb.append("<div id=\"dynamicConditionalConfigurationItem\" style=\"display: none\">");
        sb.append("<table>");
        if (StringUtils.isNotEmpty(content)) {
            content = content.replaceAll("\\<!--(.+)--\\>", ""); // 去除注释
            Document doc = Jsoup.parse(content);
            Elements elesLi = doc.select("li");
            int eleSize = elesLi.size();
            if (eleSize > 0) { // 存在
                dealHtmlEle(request, elesLi, sb);
                content = sb.toString();
            }
        }
        out.write(content);
    }

    private void dealHtmlEle(HttpServletRequest request, Elements childEles, StringBuffer sb) {
        int childElesSize = childEles.size();
        if (childElesSize > 0) { // 存在

            childEles.forEach(childEle -> {
                sb.append("<tr>");
                sb.append(" <td>" + childEle.attr("searchFiled") + "</td>");
                sb.append(" <td>" + childEle.attr("searchName") + "</td>");
                String type = childEle.attr("type");

                if (StringUtils.isNotEmpty(type) && type.toLowerCase().equals("date")) {
                    sb.append(" <td>equal_,date_s,date_e</td>");
                    Elements selectEle = childEle.select("select");
                    if (selectEle.size() > 0) {

                        String style = selectEle.attr("style");
                        sb.append("<td><select name='conditionValue' style='height:23px;" + style
                                + "'  class='select2 conditionValue noshield'>" + selectEle.html() + "</select></td>");
                    } else {
                        String dateFmt = "";
                        String dateformat = childEle.attr("date-format");
                        if (StringUtils.isNotEmpty(dateformat)) {
                            dateFmt = "{dateFmt : '" + dateformat + "'}";
                        }
                        sb.append("<td><input type=\"text\" name=\"conditionValue\"  style='height:23px' onClick=\"WdatePicker(" + dateFmt
                                + ")\"   class=\"Wdate noshield conditionValue\"/></td>");
                    }

                } else {

                    Elements selectEle = childEle.select("select");
                    if (selectEle.size() > 0) {
                        sb.append(" <td>equal_,notEqual_</td>");
                        String style = selectEle.attr("style");
                        sb.append("<td><select  name='conditionValue' style='height:23px;" + style
                                + "'   class='select2 conditionValue noshield'>" + selectEle.html() + "</select></td>");
                    } else {
                        sb.append(" <td>equal_,notEqual_,like_,like1_,like2_</td>");
                        sb.append(
                                "<td><input type=\"text\"  style='height:23px' name=\"conditionValue\" class=\"conditionValue noshield\"/></td>");
                    }
                }
                sb.append("</tr>");
            });

        }
    }
}
